import { observe } from './observe';
import Dep from './dep';

export default function defineReactive(data, key, val) {
    const dep = new Dep()
    console.log('def',data,key);
    if (arguments.length == 2) {
        val = data[key];
    }

    let childOb = observe(val)
    Object.defineProperty(data, key, {
        enumerable: true,
        configurable: true,
        get() {
            console.log('get a');
            if (Dep.target) {
                dep.depend()
                if (childOb) {
                    childOb.dep.depend()
                }
            }
            return val
        },
        set(newValue) {
            console.log('set a');
            if (val === newValue) {
                return;
            }
            val = newValue;
            childOb = observe(newValue)
            dep.notify()
        }
    })
}